<?php
namespace app\admin\controller;
use app\admin\controller\BaseController;
use app\common\errcode\AdminErrCode;
use app\common\errcode\AdminErrMsg;
use think\Loader;

class ContentController extends BaseController
{
    protected $contentModel;

    public function __construct()
    {
        parent::__construct();
        $this->contentModel = new \app\admin\model\ContentModel();
    }

    /**
     * 预设内容详情
     * @param int $type
     * @param int $toolid
     */
    public function info()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Content');
                $check = $validate->scene('info')->check($data);
                if(!$check){
                    return $this->jsonJump(AdminErrCode::PARAM_ERROR,AdminErrMsg::PARAM_ERROR.','.$validate->getError());  
                }
                return $this->jsonJump(AdminErrCode::SUCCESS,AdminErrMsg::SUCCESS,$this->contentModel->info($data));    
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->uid,$e,$this->request->path());
            return $this->jsonJump(AdminErrCode::SYSTEM_ERROR,AdminErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }

    /**
     * 编辑预设内容
     * @param int       $type
     * @param int       $toolid
     * @param string    $content
     */
    public function edit()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Content');
                $check = $validate->scene('edit')->check($data);
                if(!$check){
                    return $this->jsonJump(AdminErrCode::PARAM_ERROR,AdminErrMsg::PARAM_ERROR.','.$validate->getError());  
                }
                if($this->contentModel->edit($data) === true){
                    return $this->jsonJump(AdminErrCode::SUCCESS,AdminErrMsg::SUCCESS);    
                }
                return $this->jsonJump(AdminErrCode::EDIT_CONTENT_FAIL,AdminErrMsg::EDIT_CONTENT_FAIL);     
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->uid,$e,$this->request->path());
            return $this->jsonJump(AdminErrCode::SYSTEM_ERROR,AdminErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }
}